<template>
  <div class="bottom-bar">
    <div class="check">
      <check-button :isChecked="isCheckAll" @click.native="selectAll"></check-button>
      <span>全选</span>
    </div>
    <div class="totalPrice">
      合计：￥
      <span>{{totalPrice}}</span>
    </div>
    <div class="clearing" @click="clearingClick">去结算（{{clearing}}）</div>
  </div>
</template>

<script>
import CheckButton from '@/components/content/checkbutton/CheckButton'

import { mapGetters } from 'vuex'

export default {
  components: {
    CheckButton,
  },
  computed: {
    ...mapGetters(['cartList']),
    isCheckAll() {
      // 判断全选按钮什么情况下是选中，如果购物车的商品全部是选中的状态，返回true，否则返回false
      if (this.cartList.length && this.cartList.every((item) => item.checked)) {
        return true
      }
      return false
    },
    totalPrice() {
      return this.cartList
        .filter((item) => item.checked)
        .reduce((preValue, item) => {
          return preValue + item.count * item.price
        }, 0)
        .toFixed(2)
    },
    clearing() {
      return this.cartList
        .filter((item) => item.checked)
        .reduce((preValue, item) => {
          return preValue + item.count
        }, 0)
    },
  },
  methods: {
    // 监听判断全选按钮的点击
    selectAll() {
      if (this.isCheckAll) {
        // 如果全选按钮是选中状态(isCheckAll返回的是true),则对cartList中的商品的checked全部改为false，反之
        this.cartList.map((item) => (item.checked = false))
        // this.cartList.foreach(item => item.checked = false)
      } else {
        this.cartList.map((item) => (item.checked = true))
        // this.cartList.foreach(item => item.checked = true)
      }
    },
    // 结算时没有选中商品时的弹窗信息
    clearingClick() {
      if (!this.cartList.find((item) => item.checked))
        this.$toast.show('没有选择任何商品', 2000)
    },
  },
}
</script>

<style scoped>
.bottom-bar {
  height: 35px;
  position: relative;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 0 0 8px;
  background-color: #f6f6f6;
}
.bottom-bar .check {
  width: 70px;
  display: flex;
}
.bottom-bar .check span {
  margin-left: 8px;
}
.bottom-bar .totalPrice {
  flex: 1;
  margin-left: 8px;
}
.totalPrice span {
  color: var(--color-high-text);
  font-weight: 700;
  font-size: 18px;
}
.bottom-bar .clearing {
  width: 90px;
  height: 100%;
  line-height: 35px;
  text-align: center;
  color: #fff;
  background-color: red;
}
</style>